/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.SOP;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import managedbean.Workspace.WorkspaceManagerBean;
import merlion.common.session.stateless.SystemUserSessionBeanLocal;
import merlion.crm.sopm.entity.SalesOrder;
import merlion.crm.sopm.session.SOPSessionBeanLocal;

/**
 *
 * @author Zhang Ying
 */
@ManagedBean
@ViewScoped
public class DocumentFlowManagerBean {

    @EJB
    private SOPSessionBeanLocal salesSession;
    @EJB
    private SystemUserSessionBeanLocal userSession;
    @ManagedProperty(value = "#{workspaceManagerBean}")
    private WorkspaceManagerBean workspaceManagerBean;
    private Collection<SalesOrder> allSalesOrders;
    private String salesOrderIdStr;
    private SalesOrder selectedSalesOrder;

    /** Creates a new instance of DocumentFlowManagerBean */
    public DocumentFlowManagerBean() {
    }

    public String selectSalesOrder() {
        return null;
    }

    // getters and setters
    public Collection<SalesOrder> getAllSalesOrders() {
        if (allSalesOrders == null) {
            Collection<SalesOrder> sos = salesSession.getAllSalesOrders();
            List<SalesOrder> result = new ArrayList<SalesOrder>();
            Long staffId = workspaceManagerBean.getCurrentStaff().getId();
            
            if (userSession.checkStaffIsSalesMgmtLevel(staffId) || userSession.checkStaffIsFinMgmtLevel(staffId) ) {
                result = new ArrayList<SalesOrder>(sos);
            } else {
                
                for (SalesOrder so : sos) {
                    if (so.getCustomer().getCustomerExecutive().getId().equals(staffId)) {
                        result.add(so);
                    }
                }
            }
            allSalesOrders = result;
        }
        return allSalesOrders;
    }

    public void setAllSalesOrders(Collection<SalesOrder> allSalesOrders) {
        this.allSalesOrders = allSalesOrders;
    }

    public String getSalesOrderIdStr() {
        return salesOrderIdStr;
    }

    public void setSalesOrderIdStr(String salesOrderIdStr) {
        this.salesOrderIdStr = salesOrderIdStr;
    }

    public SalesOrder getSelectedSalesOrder() {
        return selectedSalesOrder;
    }

    public void setSelectedSalesOrder(SalesOrder selectedSalesOrder) {
        this.selectedSalesOrder = selectedSalesOrder;
        this.salesOrderIdStr = selectedSalesOrder.getSalesOrderIdStr();
    }

    // for rendering
    public boolean renderDetailPanel() {
        if (selectedSalesOrder == null) {
            return false;
        } else {
            return true;
        }
    }

    public boolean renderViewSO() {
        return renderDetailPanel();
    }

    public boolean renderViewSQ() {
        if (renderViewSO()
                && selectedSalesOrder.getPurchaseOrder() != null
                && selectedSalesOrder.getPurchaseOrder().getSalesQuotation() != null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean renderViewSQUploaded() {
        if (renderViewSQ()
                && selectedSalesOrder.getPurchaseOrder().getSalesQuotation().getUploadedImage() != null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean renderViewPOUploaded() {
        if (renderViewSO()
                && selectedSalesOrder.getPurchaseOrder() != null
                && selectedSalesOrder.getPurchaseOrder().getUploadedImage() != null) {
            return true;
        } else {
            return false;
        }
    }
    
    public boolean renderViewInvoice() {
        if (renderViewSO()
                && selectedSalesOrder.getDeliveryOrders() != null) {
            return true;
        } else {
            return false;
        }
    }

    public WorkspaceManagerBean getWorkspaceManagerBean() {
        return workspaceManagerBean;
    }

    public void setWorkspaceManagerBean(WorkspaceManagerBean workspaceManagerBean) {
        this.workspaceManagerBean = workspaceManagerBean;
    }
}
